package com.moonLight.weblog.common.utils;

import com.moonLight.weblog.common.exception.BaseExceptionInterface;
import com.moonLight.weblog.common.exception.BizException;
import lombok.Data;

import java.io.Serializable;

@Data
public class Response<T> implements Serializable {

    private  boolean success  = true;
    private  String  code ;
    private  String message ;
    private  T data;



    /**
     * 响应成功
     *
     * @param data 响应结果
     * @param <T>  响应数据类型
     * @return 响应对象
     */
    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setSuccess(true);
        response.setData(data);
        return response;
    }

    /**
     * 响应成功
     *
     * @param <T> 响应数据类型
     * @return 响应对象
     */
    public static <T> Response<T> success() {
        Response<T> response = new Response<>();
        response.setSuccess(true);
        return response;
    }

    /**
     * 响应失败
     *
     * @param code    响应状态码
     * @param message 响应消息
     * @param <T>     响应数据类型
     * @return 响应对象
     */
    public static <T> Response<T> fail(String code, String message) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    /**
     * 响应失败
     *
     * @param message 响应消息
     * @param <T>     响应数据类型
     * @return 响应对象
     */
    public static <T> Response<T> fail(String message) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(message);
        return response;
    }

    /**
     * 响应失败
     *
     * @param <T> 响应数据类型
     * @return 响应对象
     */
    public static <T> Response<T> fail() {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        return response;
    }

    /**
     * 响应失败
     *
     * @param bizException 业务异常
     * @param <T>          响应数据类型
     * @return 响应对象
     */
    public static <T> Response<T> fail(BizException bizException) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setCode(bizException.getErrorCode());
        response.setMessage(bizException.getErrorMessage());
        return response;
    }

    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setCode(baseExceptionInterface.getErrorCode());
        response.setMessage(baseExceptionInterface.getErrorMessage());
        return response;
    }



}
